package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.ShopBasic;
import com.caocaog.market.entity.ShopChannel;
import com.caocaog.market.entity.ShopChannelshop;
import com.caocaog.market.mapper.ShopChannelshopMapper;
import com.caocaog.market.service.IShopChannelService;
import com.caocaog.market.service.IShopChannelshopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.CheckFailedException;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 频道店铺关联表
 *
 * @author jeecg-boot
 * @date 2021-11-23
 */
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class ShopChannelshopServiceImpl extends ServiceImpl<ShopChannelshopMapper, ShopChannelshop> implements IShopChannelshopService {

    private CommonAPI commonAPI;
    @Autowired
    private IShopChannelService shopChannelService;
    @Autowired
    private IShopChannelshopService shopChannelshopService;
    @Override
    public Result add(ShopBasic shopBasic) {
        String[] ids = shopBasic.getChannelId().split(",");
        List<ShopChannelshop> shopChannelshops = new ArrayList<>();
        for (String id : ids){
            //check 如果该店铺已有该频道 跳过
            ShopChannel shopChannel = shopChannelService.getById(id);
            /*if ("0".equals(shopChannel.getParentId())){
                return Result.error("请选择二级频道哟~");
            }*/
            List<ShopChannelshop> check = shopChannelshopService.list(new QueryWrapper<ShopChannelshop>().eq("shop_id",shopBasic.getId()));
            if (oConvertUtils.isNotEmpty(check)){
                continue;
            }
            ShopChannelshop channelshop = new ShopChannelshop();
            channelshop.setChannelId(id);
            channelshop.setShopId(shopBasic.getId());
            shopChannelshops.add(channelshop);
        }
        shopChannelshopService.saveBatch(shopChannelshops);
        return Result.OK("添加成功");
    }

    @Override
    public Result update(ShopBasic shopBasic) {
        String[] ids = shopBasic.getChannelId().split(",");
        log.info("门店修改频道："+shopBasic.getChannelId());
        List<ShopChannelshop> shopChannelshops = new ArrayList<>();
        for (String id : ids){
            //check 如果该店铺已有该频道 跳过
            ShopChannel shopChannel = shopChannelService.getById(id);
            if (null == shopChannel){
                throw new CheckFailedException("频道不存在");
            }
            /*if ("0".equals(shopChannel.getParentId())){
                return Result.error("请选择二级频道哟~");
            }*/
            ShopChannelshop channelshop = new ShopChannelshop();
            channelshop.setChannelId(id);
            channelshop.setShopId(shopBasic.getId());
            shopChannelshops.add(channelshop);
        }
        List idList = shopChannelshopService.list(new QueryWrapper<ShopChannelshop>().eq("shop_id",shopBasic.getId()))
                .stream().map(ShopChannelshop::getId).collect(Collectors.toList());;
        shopChannelshopService.removeByIds(idList);
        shopChannelshopService.saveBatch(shopChannelshops);
        return Result.OK("修改成功");
    }

    @Override
    public List<String> getShopIdsByChannelIdSiteId(String channelIds, String siteId) {
        return super.baseMapper.getShopIdsByChannelIdSiteId(channelIds, siteId);
    }
}
